package com.syh.jn.scm.service.impl.operation.statistics;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.jn.scm.domain.operation.entity.project.OperationProject;
import com.syh.jn.scm.domain.operation.repository.project.OperationProjectRepository;
import com.syh.jn.scm.dto.operation.project.OperationProjectVo;
import com.syh.jn.scm.dto.operation.statistics.SupplierDirectProjectReportVo;
import com.syh.jn.scm.infrastructure.repository.utils.MybatisTemplate;
import com.syh.jn.scm.service.api.operation.statistics.SupplierDirProReportService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SupplierDirProReportServiceImpl implements SupplierDirProReportService {
    @Autowired
    private OperationProjectRepository operationProjectRepository;
    /**
     * 查询服务商直营项目报表数据
     *
     * @param params
     * @return
     */
    @Override
    public List<SupplierDirectProjectReportVo> supplierDirectProjectReport(Map<String, Object> params) {

        List subProjectIds = new ArrayList<Long>();

        List<OperationProjectVo> projects = operationProjectRepository.findListPageView(
                Wrapper.build().append(Restrictions.eq("businessEntityId",params.get("businessEntityId"))),1,(Integer)params.get("pageSize"));

        projects.forEach(project -> {
            Wrapper we = Wrapper.build();
            we.append(Restrictions.rightLike("catalogCode",project.getCatalogCode()+"-"));
            List<OperationProject> subProjects =  operationProjectRepository.getList(we);
            subProjects.forEach(subProject ->{
                subProjectIds.add(subProject.getId());
            });
        });
        params.put("projectIds",subProjectIds);
        List<SupplierDirectProjectReportVo> a = MybatisTemplate.selectList("supplierDirProReportQRepository.supplierDirectProjectReport",params);
        return MybatisTemplate.selectList("supplierDirProReportQRepository.supplierDirectProjectReport",params);
    }

    /**
     * 查询服务商直营项目总条数
     * @param params
     * @return
     */
    @Override
    public Integer getSupplierDirectProjectCount(Map<String, Object> params) {
        List subProjectIds = new ArrayList<Long>();

        List<OperationProjectVo> projects = operationProjectRepository.findListPageView(
                Wrapper.build().append(Restrictions.eq("businessEntityId",params.get("businessEntityId"))),1,(Integer)params.get("pageSize"));

        projects.forEach(project -> {
            Wrapper we = Wrapper.build();
            we.append(Restrictions.rightLike("catalogCode",project.getCatalogCode()+"-"));
            List<OperationProject> subProjects =  operationProjectRepository.getList(we);
            subProjects.forEach(subProject ->{
                subProjectIds.add(subProject.getId());
            });
        });
        params.put("projectIds",subProjectIds);
        return MybatisTemplate.selectOne("supplierDirProReportQRepository.getSupplierDirectProjectCount",params);
    }
}
